package com.it.controller.admin;


import com.it.domain.BlogTagEntity;
import com.it.service.BlogTagService;
import com.it.util.PageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

/**
 * @author : code1997
 * @date : 2021/9/9 22:46
 */
@Slf4j
@Controller
@RequestMapping("/admin")
public class BlogTagController {

    private static final String TAGS_URL = "admin/tags";
    private static final String TAGS_INPUT_URL = "admin/tags-input";

    @Autowired
    private BlogTagService blogTagService;

    @GetMapping("/tags")
    public String getTags(@RequestParam(defaultValue = "1") String page, Model model) {
        Map<String, Object> params = new HashMap<>();
        params.put("page", page);
        PageUtils curPage = blogTagService.listTag(params);
        model.addAttribute("page", curPage);
        return TAGS_URL;
    }

    @GetMapping("/tags/input")
    public String toTagsInput(Model model) {
        log.info("前往添加页面");
        model.addAttribute("tag", new BlogTagEntity());
        return TAGS_INPUT_URL;
    }

    @PostMapping("tags")
    public String saveTag(@Valid BlogTagEntity blogTagEntity, BindingResult result, RedirectAttributes attributes) {
        if (blogTagService.getTagByName(blogTagEntity.getName()) != null) {
            result.rejectValue("name", "nameError", "该标签名称已经存在，不能重复添加！！！");
        }
        if (result.hasErrors()) {
            return TAGS_INPUT_URL;
        }
        BlogTagEntity blogTagEntity1 = blogTagService.saveTag(blogTagEntity);
        if (blogTagEntity1 == null) {
            attributes.addFlashAttribute("message", "添加失败,分类的名称为【" + blogTagEntity.getName() + "】！！！");
        } else {
            attributes.addFlashAttribute("message", "添加分类【" + blogTagEntity.getName() + "】成功！！！");
        }
        return "redirect:/" + TAGS_URL;
    }

    /**
     * 调转到types的修改页面，在修改页面之前获取到需要修改的数据，显示到页面中去
     *
     * @param id    ：需要修改的类型的id。
     * @param model ：需要返回的数据。
     * @return ：需要调转到的页面
     */
    @GetMapping("/tags/{id}/input")
    public String editInput(@PathVariable("id") Long id, Model model) {
        model.addAttribute("tag", blogTagService.getTag(id));
        return TAGS_INPUT_URL;
    }

    @PostMapping("/tags/{id}")
    public String editTag(@Valid BlogTagEntity blogTagEntity, BindingResult result, @PathVariable("id") Long id, RedirectAttributes attributes) {
        /*该校验需要hasErrors之前*/
        if (blogTagService.getTagByName(blogTagEntity.getName()) != null) {
            result.rejectValue("name", "nameError", "该标签名称已经存在，不能重复添加！！！");
        }
        if (result.hasErrors()) {
            return TAGS_INPUT_URL;
        }
        BlogTagEntity blogTagEntity1 = blogTagService.updateTag(id, blogTagEntity);
        if (blogTagEntity1 == null) {
            attributes.addFlashAttribute("message", "修改失败,分类的名称为【" + blogTagEntity.getName() + "】！！！");
        } else {
            attributes.addFlashAttribute("message", "修改分类【" + blogTagEntity.getName() + "】成功！！！");
        }
        return "redirect:/" + TAGS_URL;
    }

    @GetMapping("/tags/{id}/delete")
    public String delete(@PathVariable Long id, RedirectAttributes attributes) {
        blogTagService.deleteTag(id);
        attributes.addFlashAttribute("message", "删除标签成功！！！");
        return "redirect:/" + TAGS_URL;
    }

}
